
#include<iostream>

#include "BinaryQueue.h"

using namespace std;

int main()
{
	BinaryQueue queue;

	std::thread worker;
	worker = thread([&]() { 
		
		Message* msg;
		while (true)
		{

			msg = queue.dequeueBusy();

			if (msg == nullptr)
			{
				std::cout << "Wroker exit!\n";
				return;
			}

			this_thread::sleep_for(std::chrono::milliseconds(2000));
			std::cout << "------Handled Message:" << msg->msg << std::endl;

			queue.enqueueIdle(msg);

			std::cout << "\n\n";
		}
	});



	std::string s;

	Message* msg;

	while (true)
	{
		std::cin >> s;
		if (s != "x")
		{
			msg = queue.dequeueIdle();

			if (msg == nullptr)
			{
				std::cout << "Input Exit!\n";
				break;
			}

			msg->msg = s;

			queue.enqueueBusy(msg);
		}
		else
		{
			break;
		}
	}

	queue.enqueueIdle(nullptr);
	queue.enqueueBusy(nullptr);

	worker.join();

	std::cout << "exit!\n";

	return 0;
}
